{ "cells": [ { "cell_type": "markdown", "id": "79d9aaef", "metadata": {}, "source": [ "# Working with Market Depth and Trades" ] }, { "cell_type": "markdown", "id": "d0585554", "metadata": {}, "source": [ "## Display 3-depth" ] }, { "cell_type": "code", "execution_count": 1, "id": "8bbb4d6f-7a84-4fec-ac56-24a3d2b3d78a", "metadata": {}, "outputs": [], "source": [ "from numba import njit\n", "\n", "@njit\n", "def print_3depth(hbt):\n", " while hbt.elapse(60_000_000_000) == 0:\n", " print('current_timestamp:', hbt.current_timestamp)\n", "\n", " # Gets the market depth for the first asset, in the same order as when you created the backtest.\n", " depth = hbt.depth(0)\n", "\n", " # a key of bid_depth or ask_depth is price in ticks.\n", " # (integer) price_tick = rice / tick_size\n", " i = 0\n", " for price_tick in range(depth.best_ask_tick, depth.best_ask_tick + 100):\n", " qty = depth.ask_qty_at_tick(price_tick)\n", " if qty > 0:\n", " print(\n", " 'ask: ',\n", " qty,\n", " '@',\n", " np.round(price_tick * depth.tick_size, 1)\n", " )\n", " \n", " i += 1\n", " if i == 3:\n", " break\n", " i = 0\n", " for price_tick in range(depth.best_bid_tick, max(depth.best_bid_tick - 100, 0), -1):\n", " qty = depth.bid_qty_at_tick(price_tick)\n", " if qty > 0:\n", " print(\n", " 'bid: ',\n", " qty,\n", " '@',\n", " np.round(price_tick * depth.tick_size, 1)\n", " )\n", " \n", " i += 1\n", " if i == 3:\n", " break\n", " return True" ] }, { "cell_type": "code", "execution_count": 2, "id": "0aab2f88", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "btcusdt_20240809 = np.load('usdm/btcusdt_20240809.npz')['data']\n", "btcusdt_20240808_eod = np.load('usdm/btcusdt_20240808_eod.npz')['data']" ] }, { "cell_type": "code", "execution_count": 3, "id": "79afc7c0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "current_timestamp: 1723161661500000000\n", "ask: 1.759 @ 61594.2\n", "ask: 0.006 @ 61594.4\n", "ask: 0.114 @ 61595.2\n", "bid: 3.526 @ 61594.1\n", "bid: 0.016 @ 61594.0\n", "bid: 0.002 @ 61593.9\n", "current_timestamp: 1723161721500000000\n", "ask: 2.575 @ 61576.6\n", "ask: 0.004 @ 61576.7\n", "ask: 0.455 @ 61577.0\n", "bid: 2.558 @ 61576.5\n", "bid: 0.002 @ 61576.0\n", "bid: 0.515 @ 61575.5\n", "current_timestamp: 1723161781500000000\n", "ask: 0.131 @ 61629.7\n", "ask: 0.005 @ 61630.1\n", "ask: 0.005 @ 61630.5\n", "bid: 5.742 @ 61629.6\n", "bid: 0.247 @ 61629.4\n", "bid: 0.034 @ 61629.3\n", "current_timestamp: 1723161841500000000\n", "ask: 0.202 @ 61621.6\n", "ask: 0.002 @ 61622.5\n", "ask: 0.003 @ 61622.6\n", "bid: 3.488 @ 61621.5\n", "bid: 0.86 @ 61620.0\n", "bid: 0.248 @ 61619.6\n", "current_timestamp: 1723161901500000000\n", "ask: 1.397 @ 61584.0\n", "ask: 0.832 @ 61585.1\n", "ask: 0.132 @ 61586.0\n", "bid: 3.307 @ 61583.9\n", "bid: 0.01 @ 61583.8\n", "bid: 0.002 @ 61582.0\n" ] } ], "source": [ "from hftbacktest import BacktestAsset, HashMapMarketDepthBacktest\n", "\n", "asset = (\n", " BacktestAsset()\n", " .data(btcusdt_20240809)\n", " .initial_snapshot(btcusdt_20240808_eod)\n", " .linear_asset(1.0) \n", " .constant_latency(10_000_000, 10_000_000)\n", " .risk_adverse_queue_model() \n", " .no_partial_fill_exchange()\n", " .trading_value_fee_model(0.0002, 0.0007)\n", " .tick_size(0.1)\n", " .lot_size(0.001)\n", ")\n", "\n", "hbt = HashMapMarketDepthBacktest([asset])\n", "\n", "print_3depth(hbt)\n", "\n", "_ = hbt.close()" ] }, { "cell_type": "markdown", "id": "354e8fec-95cb-4205-b820-f934fa2d5836", "metadata": {}, "source": [ "## Efficient Market Depth Access\n", "\n", "`ROIVectorMarketDepth` provides more efficient market depth access through a vector that holds a limited price range of interest. The backtester using this feature can be created by `ROIVectorMarketDepthBacktest`." ] }, { "cell_type": "code", "execution_count": 4, "id": "580a566c-6520-40b0-90ec-c2a933017b52", "metadata": {}, "outputs": [], "source": [ "from numba import njit\n", "\n", "@njit\n", "def print_3depth_fast(hbt):\n", " roi_lb_tick = int(round(30000 / 0.1))\n", " roi_ub_tick = int(round(90000 / 0.1))\n", " \n", " while hbt.elapse(60_000_000_000) == 0:\n", " print('current_timestamp:', hbt.current_timestamp)\n", "\n", " # Gets the market depth for the first asset, in the same order as when you created the backtest.\n", " depth = hbt.depth(0)\n", "\n", " # a key of bid_depth or ask_depth is price in ticks.\n", " # (integer) price_tick = price / tick_size\n", " i = 0\n", " # for price_tick in range(depth.best_ask_tick, depth.best_ask_tick + 100):\n", " # # depth.ask_depth returns the ask depth array, whose length is (roi_ub_tick + 1 - roi_lb_tick),\n", " # # containing the quantities ranging from roi_lb_tick to roi_ub_tick.\n", " # # Checks that the price_tick is in that range and adjust the index by subtracting roi_lb_tick.\n", " # if price_tick < roi_lb_tick or price_tick > roi_ub_tick:\n", " # continue\n", " # t = price_tick - roi_lb_tick\n", " # qty = depth.ask_depth[t]\n", " # if qty > 0:\n", " # print(\n", " # 'ask: ',\n", " # qty,\n", " # '@',\n", " # np.round(price_tick * depth.tick_size, 1)\n", " # )\n", " \n", " # i += 1\n", " # if i == 3:\n", " # break\n", " # i = 0\n", " # for price_tick in range(depth.best_bid_tick, max(depth.best_bid_tick - 100, 0), -1):\n", " # # depth.bid_depth returns the bid depth array, whose length is (roi_ub_tick + 1 - roi_lb_tick),\n", " # # containing the quantities ranging from roi_lb_tick to roi_ub_tick.\n", " # # Checks that the price_tick is in that range and adjust the index by subtracting roi_lb_tick.\n", " # if price_tick < roi_lb_tick or price_tick > roi_ub_tick:\n", " # continue\n", " # t = price_tick - roi_lb_tick\n", " # qty = depth.bid_depth[t]\n", " # if qty > 0:\n", " # print(\n", " # 'bid: ',\n", " # qty,\n", " # '@',\n", " # np.round(price_tick * depth.tick_size, 1)\n", " # )\n", " \n", " # i += 1\n", " # if i == 3:\n", " # break\n", " return True" ] }, { "cell_type": "code", "execution_count": 5, "id": "94d2e7a7-9179-4380-8e18-661833a4b95f", "metadata": {}, "outputs": [], "source": [ "from hftbacktest import ROIVectorMarketDepthBacktest\n", "\n", "asset = (\n", " BacktestAsset()\n", " .data(btcusdt_20240809)\n", " .initial_snapshot(btcusdt_20240808_eod)\n", " .linear_asset(1.0) \n", " .constant_latency(10_000_000, 10_000_000)\n", " .risk_adverse_queue_model() \n", " .no_partial_fill_exchange()\n", " .trading_value_fee_model(0.0002, 0.0007)\n", " .tick_size(0.1)\n", " .lot_size(0.001)\n", " # Sets the lower bound price for the range of interest in the market depth.\n", " .roi_lb(30000)\n", " # Sets the upper bound price for the range of interest in the market depth.\n", " .roi_ub(90000)\n", ")\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "a8e2b8bc-eb4f-4401-9d1c-d4e5428708e4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "current_timestamp: 1723161661500000000\n", "current_timestamp: 1723161721500000000\n", "current_timestamp: 1723161781500000000\n", "current_timestamp: 1723161841500000000\n", "current_timestamp: 1723161901500000000\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hbt = ROIVectorMarketDepthBacktest([asset])\n", "\n", "print_3depth_fast(hbt)\n", "\n", "#_ = hbt.close()" ] }, { "cell_type": "markdown", "id": "a8f00571", "metadata": {}, "source": [ "## Order Book Imbalance" ] }, { "cell_type": "code", "execution_count": 7, "id": "e8d55680", "metadata": {}, "outputs": [], "source": [ "@njit\n", "def orderbookimbalance(hbt, out):\n", " roi_lb_tick = int(round(30000 / 0.1))\n", " roi_ub_tick = int(round(90000 / 0.1))\n", "\n", " while hbt.elapse(10 * 1e9) == 0:\n", " depth = hbt.depth(0)\n", " \n", " mid_price = (depth.best_bid + depth.best_ask) / 2.0\n", " \n", " sum_ask_qty_50bp = 0.0\n", " sum_ask_qty = 0.0\n", " for price_tick in range(depth.best_ask_tick, roi_ub_tick + 1):\n", " if price_tick < roi_lb_tick or price_tick > roi_ub_tick:\n", " continue\n", " t = price_tick - roi_lb_tick\n", " \n", " ask_price = price_tick * depth.tick_size\n", " depth_from_mid = (ask_price - mid_price) / mid_price\n", " if depth_from_mid > 0.01:\n", " break\n", " sum_ask_qty += depth.ask_depth[t]\n", " \n", " if depth_from_mid <= 0.005:\n", " sum_ask_qty_50bp = sum_ask_qty\n", " \n", " \n", " sum_bid_qty_50bp = 0.0\n", " sum_bid_qty = 0.0\n", " for price_tick in range(depth.best_bid_tick, roi_lb_tick - 1, -1):\n", " if price_tick < roi_lb_tick or price_tick > roi_ub_tick:\n", " continue\n", " t = price_tick - roi_lb_tick\n", " \n", " bid_price = price_tick * depth.tick_size\n", " depth_from_mid = (mid_price - bid_price) / mid_price\n", " if depth_from_mid > 0.01:\n", " break\n", " sum_bid_qty += depth.bid_depth[t]\n", " \n", " if depth_from_mid <= 0.005:\n", " sum_bid_qty_50bp = sum_bid_qty\n", " \n", " imbalance_50bp = sum_bid_qty_50bp - sum_ask_qty_50bp\n", " imbalance_1pct = sum_bid_qty - sum_ask_qty\n", " imbalance_tob = depth.bid_depth[depth.best_bid_tick - roi_lb_tick] - depth.ask_depth[depth.best_ask_tick - roi_lb_tick]\n", " \n", " out.append((hbt.current_timestamp, imbalance_tob, imbalance_50bp, imbalance_1pct)) \n", " return True" ] }, { "cell_type": "code", "execution_count": 8, "id": "99a684f8", "metadata": {}, "outputs": [], "source": [ "from numba.typed import List\n", "from numba.types import Tuple, float64\n", "\n", "hbt = ROIVectorMarketDepthBacktest([asset])\n", "\n", "tup_ty = Tuple((float64, float64, float64, float64))\n", "out = List.empty_list(tup_ty, allocated=100_000)\n", "\n", "orderbookimbalance(hbt, out)\n", "\n", "_ = hbt.close()" ] }, { "cell_type": "code", "execution_count": 9, "id": "be1b53b2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "shape: (30, 4)
Local TimestampTOB Imbalance0.5% Imbalance1% Imbalance
datetime[ns]f64f64f64
2024-08-09 00:00:11.5002.729-1748.101-3908.736
2024-08-09 00:00:21.5004.623-1749.435-3512.845
2024-08-09 00:00:31.500-6.465-1259.897-3357.755
2024-08-09 00:00:41.500-7.922-1174.185-3471.955
2024-08-09 00:00:51.500-2.484-1147.597-3461.48
2024-08-09 00:04:21.5003.828-1186.236-3551.78
2024-08-09 00:04:31.500-1.35-1332.379-3517.854
2024-08-09 00:04:41.500-3.754-1166.521-2693.672
2024-08-09 00:04:51.500-2.525-1188.56-2716.914
2024-08-09 00:05:01.5001.91-594.991-2138.82
" ], "text/plain": [ "shape: (30, 4)\n", "┌─────────────────────────┬───────────────┬────────────────┬──────────────┐\n", "│ Local Timestamp ┆ TOB Imbalance ┆ 0.5% Imbalance ┆ 1% Imbalance │\n", "│ --- ┆ --- ┆ --- ┆ --- │\n", "│ datetime[ns] ┆ f64 ┆ f64 ┆ f64 │\n", "╞═════════════════════════╪═══════════════╪════════════════╪══════════════╡\n", "│ 2024-08-09 00:00:11.500 ┆ 2.729 ┆ -1748.101 ┆ -3908.736 │\n", "│ 2024-08-09 00:00:21.500 ┆ 4.623 ┆ -1749.435 ┆ -3512.845 │\n", "│ 2024-08-09 00:00:31.500 ┆ -6.465 ┆ -1259.897 ┆ -3357.755 │\n", "│ 2024-08-09 00:00:41.500 ┆ -7.922 ┆ -1174.185 ┆ -3471.955 │\n", "│ 2024-08-09 00:00:51.500 ┆ -2.484 ┆ -1147.597 ┆ -3461.48 │\n", "│ … ┆ … ┆ … ┆ … │\n", "│ 2024-08-09 00:04:21.500 ┆ 3.828 ┆ -1186.236 ┆ -3551.78 │\n", "│ 2024-08-09 00:04:31.500 ┆ -1.35 ┆ -1332.379 ┆ -3517.854 │\n", "│ 2024-08-09 00:04:41.500 ┆ -3.754 ┆ -1166.521 ┆ -2693.672 │\n", "│ 2024-08-09 00:04:51.500 ┆ -2.525 ┆ -1188.56 ┆ -2716.914 │\n", "│ 2024-08-09 00:05:01.500 ┆ 1.91 ┆ -594.991 ┆ -2138.82 │\n", "└─────────────────────────┴───────────────┴────────────────┴──────────────┘" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import polars as pl\n", "\n", "df = pl.DataFrame(out).transpose()\n", "df.columns = ['Local Timestamp', 'TOB Imbalance', '0.5% Imbalance', '1% Imbalance']\n", "df = df.with_columns(\n", " pl.from_epoch('Local Timestamp', time_unit='ns')\n", ")\n", "\n", "df" ] }, { "cell_type": "code", "execution_count": 10, "id": "f3b43c9f", "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "(function(root) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = true;\n", " var py_version = '3.4.2'.replace('rc', '-rc.').replace('.dev', '-dev.');\n", " var reloading = false;\n", " var Bokeh = root.Bokeh;\n", "\n", " if (typeof (root._bokeh_timeout) === \"undefined\" || force) {\n", " root._bokeh_timeout = Date.now() + 5000;\n", " root._bokeh_failed_load = false;\n", " }\n", "\n", " function run_callbacks() {\n", " try {\n", " root._bokeh_onload_callbacks.forEach(function(callback) {\n", " if (callback != null)\n", " callback();\n", " });\n", " } finally {\n", " delete root._bokeh_onload_callbacks;\n", " }\n", " console.debug(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(css_urls, js_urls, js_modules, js_exports, callback) {\n", " if (css_urls == null) css_urls = [];\n", " if (js_urls == null) js_urls = [];\n", " if (js_modules == null) js_modules = [];\n", " if (js_exports == null) js_exports = {};\n", "\n", " root._bokeh_onload_callbacks.push(callback);\n", "\n", " if (root._bokeh_is_loading > 0) {\n", " console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls.length === 0 && js_modules.length === 0 && Object.keys(js_exports).length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " if (!reloading) {\n", " console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " }\n", "\n", " function on_load() {\n", " root._bokeh_is_loading--;\n", " if (root._bokeh_is_loading === 0) {\n", " console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n", " run_callbacks()\n", " }\n", " }\n", " window._bokeh_on_load = on_load\n", "\n", " function on_error() {\n", " console.error(\"failed to load \" + url);\n", " }\n", "\n", " var skip = [];\n", " if (window.requirejs) {\n", " window.requirejs.config({'packages': {}, 'paths': {}, 'shim': {}});\n", " root._bokeh_is_loading = css_urls.length + 0;\n", " } else {\n", " root._bokeh_is_loading = css_urls.length + js_urls.length + js_modules.length + Object.keys(js_exports).length;\n", " }\n", "\n", " var existing_stylesheets = []\n", " var links = document.getElementsByTagName('link')\n", " for (var i = 0; i < links.length; i++) {\n", " var link = links[i]\n", " if (link.href != null) {\n", "\texisting_stylesheets.push(link.href)\n", " }\n", " }\n", " for (var i = 0; i < css_urls.length; i++) {\n", " var url = css_urls[i];\n", " if (existing_stylesheets.indexOf(url) !== -1) {\n", "\ton_load()\n", "\tcontinue;\n", " }\n", " const element = document.createElement(\"link\");\n", " element.onload = on_load;\n", " element.onerror = on_error;\n", " element.rel = \"stylesheet\";\n", " element.type = \"text/css\";\n", " element.href = url;\n", " console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n", " document.body.appendChild(element);\n", " } var existing_scripts = []\n", " var scripts = document.getElementsByTagName('script')\n", " for (var i = 0; i < scripts.length; i++) {\n", " var script = scripts[i]\n", " if (script.src != null) {\n", "\texisting_scripts.push(script.src)\n", " }\n", " }\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " if (skip.indexOf(url) !== -1 || existing_scripts.indexOf(url) !== -1) {\n", "\tif (!window.requirejs) {\n", "\t on_load();\n", "\t}\n", "\tcontinue;\n", " }\n", " var element = document.createElement('script');\n", " element.onload = on_load;\n", " element.onerror = on_error;\n", " element.async = false;\n", " element.src = url;\n", " console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.head.appendChild(element);\n", " }\n", " for (var i = 0; i < js_modules.length; i++) {\n", " var url = js_modules[i];\n", " if (skip.indexOf(url) !== -1 || existing_scripts.indexOf(url) !== -1) {\n", "\tif (!window.requirejs) {\n", "\t on_load();\n", "\t}\n", "\tcontinue;\n", " }\n", " var element = document.createElement('script');\n", " element.onload = on_load;\n", " element.onerror = on_error;\n", " element.async = false;\n", " element.src = url;\n", " element.type = \"module\";\n", " console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.head.appendChild(element);\n", " }\n", " for (const name in js_exports) {\n", " var url = js_exports[name];\n", " if (skip.indexOf(url) >= 0 || root[name] != null) {\n", "\tif (!window.requirejs) {\n", "\t on_load();\n", "\t}\n", "\tcontinue;\n", " }\n", " var element = document.createElement('script');\n", " element.onerror = on_error;\n", " element.async = false;\n", " element.type = \"module\";\n", " console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " element.textContent = `\n", " import ${name} from \"${url}\"\n", " window.${name} = ${name}\n", " window._bokeh_on_load()\n", " `\n", " document.head.appendChild(element);\n", " }\n", " if (!js_urls.length && !js_modules.length) {\n", " on_load()\n", " }\n", " };\n", "\n", " function inject_raw_css(css) {\n", " const element = document.createElement(\"style\");\n", " element.appendChild(document.createTextNode(css));\n", " document.body.appendChild(element);\n", " }\n", "\n", " var js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-3.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.4.2.min.js\", \"https://cdn.holoviz.org/panel/1.4.4/dist/panel.min.js\"];\n", " var js_modules = [];\n", " var js_exports = {};\n", " var css_urls = [];\n", " var inline_js = [ function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", "function(Bokeh) {} // ensure no trailing comma for IE\n", " ];\n", "\n", " function run_inline_js() {\n", " if ((root.Bokeh !== undefined) || (force === true)) {\n", " for (var i = 0; i < inline_js.length; i++) {\n", "\ttry {\n", " inline_js[i].call(root, root.Bokeh);\n", "\t} catch(e) {\n", "\t if (!reloading) {\n", "\t throw e;\n", "\t }\n", "\t}\n", " }\n", " // Cache old bokeh versions\n", " if (Bokeh != undefined && !reloading) {\n", "\tvar NewBokeh = root.Bokeh;\n", "\tif (Bokeh.versions === undefined) {\n", "\t Bokeh.versions = new Map();\n", "\t}\n", "\tif (NewBokeh.version !== Bokeh.version) {\n", "\t Bokeh.versions.set(NewBokeh.version, NewBokeh)\n", "\t}\n", "\troot.Bokeh = Bokeh;\n", " }} else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!root._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " root._bokeh_failed_load = true;\n", " }\n", " root._bokeh_is_initializing = false\n", " }\n", "\n", " function load_or_wait() {\n", " // Implement a backoff loop that tries to ensure we do not load multiple\n", " // versions of Bokeh and its dependencies at the same time.\n", " // In recent versions we use the root._bokeh_is_initializing flag\n", " // to determine whether there is an ongoing attempt to initialize\n", " // bokeh, however for backward compatibility we also try to ensure\n", " // that we do not start loading a newer (Panel>=1.0 and Bokeh>3) version\n", " // before older versions are fully initialized.\n", " if (root._bokeh_is_initializing && Date.now() > root._bokeh_timeout) {\n", " root._bokeh_is_initializing = false;\n", " root._bokeh_onload_callbacks = undefined;\n", " console.log(\"Bokeh: BokehJS was loaded multiple times but one version failed to initialize.\");\n", " load_or_wait();\n", " } else if (root._bokeh_is_initializing || (typeof root._bokeh_is_initializing === \"undefined\" && root._bokeh_onload_callbacks !== undefined)) {\n", " setTimeout(load_or_wait, 100);\n", " } else {\n", " root._bokeh_is_initializing = true\n", " root._bokeh_onload_callbacks = []\n", " var bokeh_loaded = Bokeh != null && (Bokeh.version === py_version || (Bokeh.versions !== undefined && Bokeh.versions.has(py_version)));\n", " if (!reloading && !bokeh_loaded) {\n", "\troot.Bokeh = undefined;\n", " }\n", " load_libs(css_urls, js_urls, js_modules, js_exports, function() {\n", "\tconsole.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n", "\trun_inline_js();\n", " });\n", " }\n", " }\n", " // Give older versions of the autoload script a head-start to ensure\n", " // they initialize before we start loading newer version.\n", " setTimeout(load_or_wait, 100)\n", "}(window));" ], "application/vnd.holoviews_load.v0+json": "(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n var py_version = '3.4.2'.replace('rc', '-rc.').replace('.dev', '-dev.');\n var reloading = false;\n var Bokeh = root.Bokeh;\n\n if (typeof (root._bokeh_timeout) === \"undefined\" || force) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) {\n if (callback != null)\n callback();\n });\n } finally {\n delete root._bokeh_onload_callbacks;\n }\n console.debug(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(css_urls, js_urls, js_modules, js_exports, callback) {\n if (css_urls == null) css_urls = [];\n if (js_urls == null) js_urls = [];\n if (js_modules == null) js_modules = [];\n if (js_exports == null) js_exports = {};\n\n root._bokeh_onload_callbacks.push(callback);\n\n if (root._bokeh_is_loading > 0) {\n console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls.length === 0 && js_modules.length === 0 && Object.keys(js_exports).length === 0) {\n run_callbacks();\n return null;\n }\n if (!reloading) {\n console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n }\n\n function on_load() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n run_callbacks()\n }\n }\n window._bokeh_on_load = on_load\n\n function on_error() {\n console.error(\"failed to load \" + url);\n }\n\n var skip = [];\n if (window.requirejs) {\n window.requirejs.config({'packages': {}, 'paths': {}, 'shim': {}});\n root._bokeh_is_loading = css_urls.length + 0;\n } else {\n root._bokeh_is_loading = css_urls.length + js_urls.length + js_modules.length + Object.keys(js_exports).length;\n }\n\n var existing_stylesheets = []\n var links = document.getElementsByTagName('link')\n for (var i = 0; i < links.length; i++) {\n var link = links[i]\n if (link.href != null) {\n\texisting_stylesheets.push(link.href)\n }\n }\n for (var i = 0; i < css_urls.length; i++) {\n var url = css_urls[i];\n if (existing_stylesheets.indexOf(url) !== -1) {\n\ton_load()\n\tcontinue;\n }\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error;\n element.rel = \"stylesheet\";\n element.type = \"text/css\";\n element.href = url;\n console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n document.body.appendChild(element);\n } var existing_scripts = []\n var scripts = document.getElementsByTagName('script')\n for (var i = 0; i < scripts.length; i++) {\n var script = scripts[i]\n if (script.src != null) {\n\texisting_scripts.push(script.src)\n }\n }\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n if (skip.indexOf(url) !== -1 || existing_scripts.indexOf(url) !== -1) {\n\tif (!window.requirejs) {\n\t on_load();\n\t}\n\tcontinue;\n }\n var element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error;\n element.async = false;\n element.src = url;\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n for (var i = 0; i < js_modules.length; i++) {\n var url = js_modules[i];\n if (skip.indexOf(url) !== -1 || existing_scripts.indexOf(url) !== -1) {\n\tif (!window.requirejs) {\n\t on_load();\n\t}\n\tcontinue;\n }\n var element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error;\n element.async = false;\n element.src = url;\n element.type = \"module\";\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n for (const name in js_exports) {\n var url = js_exports[name];\n if (skip.indexOf(url) >= 0 || root[name] != null) {\n\tif (!window.requirejs) {\n\t on_load();\n\t}\n\tcontinue;\n }\n var element = document.createElement('script');\n element.onerror = on_error;\n element.async = false;\n element.type = \"module\";\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n element.textContent = `\n import ${name} from \"${url}\"\n window.${name} = ${name}\n window._bokeh_on_load()\n `\n document.head.appendChild(element);\n }\n if (!js_urls.length && !js_modules.length) {\n on_load()\n }\n };\n\n function inject_raw_css(css) {\n const element = document.createElement(\"style\");\n element.appendChild(document.createTextNode(css));\n document.body.appendChild(element);\n }\n\n var js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-3.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.4.2.min.js\", \"https://cdn.holoviz.org/panel/1.4.4/dist/panel.min.js\"];\n var js_modules = [];\n var js_exports = {};\n var css_urls = [];\n var inline_js = [ function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\nfunction(Bokeh) {} // ensure no trailing comma for IE\n ];\n\n function run_inline_js() {\n if ((root.Bokeh !== undefined) || (force === true)) {\n for (var i = 0; i < inline_js.length; i++) {\n\ttry {\n inline_js[i].call(root, root.Bokeh);\n\t} catch(e) {\n\t if (!reloading) {\n\t throw e;\n\t }\n\t}\n }\n // Cache old bokeh versions\n if (Bokeh != undefined && !reloading) {\n\tvar NewBokeh = root.Bokeh;\n\tif (Bokeh.versions === undefined) {\n\t Bokeh.versions = new Map();\n\t}\n\tif (NewBokeh.version !== Bokeh.version) {\n\t Bokeh.versions.set(NewBokeh.version, NewBokeh)\n\t}\n\troot.Bokeh = Bokeh;\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n }\n root._bokeh_is_initializing = false\n }\n\n function load_or_wait() {\n // Implement a backoff loop that tries to ensure we do not load multiple\n // versions of Bokeh and its dependencies at the same time.\n // In recent versions we use the root._bokeh_is_initializing flag\n // to determine whether there is an ongoing attempt to initialize\n // bokeh, however for backward compatibility we also try to ensure\n // that we do not start loading a newer (Panel>=1.0 and Bokeh>3) version\n // before older versions are fully initialized.\n if (root._bokeh_is_initializing && Date.now() > root._bokeh_timeout) {\n root._bokeh_is_initializing = false;\n root._bokeh_onload_callbacks = undefined;\n console.log(\"Bokeh: BokehJS was loaded multiple times but one version failed to initialize.\");\n load_or_wait();\n } else if (root._bokeh_is_initializing || (typeof root._bokeh_is_initializing === \"undefined\" && root._bokeh_onload_callbacks !== undefined)) {\n setTimeout(load_or_wait, 100);\n } else {\n root._bokeh_is_initializing = true\n root._bokeh_onload_callbacks = []\n var bokeh_loaded = Bokeh != null && (Bokeh.version === py_version || (Bokeh.versions !== undefined && Bokeh.versions.has(py_version)));\n if (!reloading && !bokeh_loaded) {\n\troot.Bokeh = undefined;\n }\n load_libs(css_urls, js_urls, js_modules, js_exports, function() {\n\tconsole.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n\trun_inline_js();\n });\n }\n }\n // Give older versions of the autoload script a head-start to ensure\n // they initialize before we start loading newer version.\n setTimeout(load_or_wait, 100)\n}(window));" }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "if ((window.PyViz === undefined) || (window.PyViz instanceof HTMLElement)) {\n", " window.PyViz = {comms: {}, comm_status:{}, kernels:{}, receivers: {}, plot_index: []}\n", "}\n", "\n", "\n", " function JupyterCommManager() {\n", " }\n", "\n", " JupyterCommManager.prototype.register_target = function(plot_id, comm_id, msg_handler) {\n", " if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n", " var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n", " comm_manager.register_target(comm_id, function(comm) {\n", " comm.on_msg(msg_handler);\n", " });\n", " } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n", " window.PyViz.kernels[plot_id].registerCommTarget(comm_id, function(comm) {\n", " comm.onMsg = msg_handler;\n", " });\n", " } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n", " google.colab.kernel.comms.registerTarget(comm_id, (comm) => {\n", " var messages = comm.messages[Symbol.asyncIterator]();\n", " function processIteratorResult(result) {\n", " var message = result.value;\n", " console.log(message)\n", " var content = {data: message.data, comm_id};\n", " var buffers = []\n", " for (var buffer of message.buffers || []) {\n", " buffers.push(new DataView(buffer))\n", " }\n", " var metadata = message.metadata || {};\n", " var msg = {content, buffers, metadata}\n", " msg_handler(msg);\n", " return messages.next().then(processIteratorResult);\n", " }\n", " return messages.next().then(processIteratorResult);\n", " })\n", " }\n", " }\n", "\n", " JupyterCommManager.prototype.get_client_comm = function(plot_id, comm_id, msg_handler) {\n", " if (comm_id in window.PyViz.comms) {\n", " return window.PyViz.comms[comm_id];\n", " } else if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n", " var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n", " var comm = comm_manager.new_comm(comm_id, {}, {}, {}, comm_id);\n", " if (msg_handler) {\n", " comm.on_msg(msg_handler);\n", " }\n", " } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n", " var comm = window.PyViz.kernels[plot_id].connectToComm(comm_id);\n", " comm.open();\n", " if (msg_handler) {\n", " comm.onMsg = msg_handler;\n", " }\n", " } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n", " var comm_promise = google.colab.kernel.comms.open(comm_id)\n", " comm_promise.then((comm) => {\n", " window.PyViz.comms[comm_id] = comm;\n", " if (msg_handler) {\n", " var messages = comm.messages[Symbol.asyncIterator]();\n", " function processIteratorResult(result) {\n", " var message = result.value;\n", " var content = {data: message.data};\n", " var metadata = message.metadata || {comm_id};\n", " var msg = {content, metadata}\n", " msg_handler(msg);\n", " return messages.next().then(processIteratorResult);\n", " }\n", " return messages.next().then(processIteratorResult);\n", " }\n", " }) \n", " var sendClosure = (data, metadata, buffers, disposeOnDone) => {\n", " return comm_promise.then((comm) => {\n", " comm.send(data, metadata, buffers, disposeOnDone);\n", " });\n", " };\n", " var comm = {\n", " send: sendClosure\n", " };\n", " }\n", " window.PyViz.comms[comm_id] = comm;\n", " return comm;\n", " }\n", " window.PyViz.comm_manager = new JupyterCommManager();\n", " \n", "\n", "\n", "var JS_MIME_TYPE = 'application/javascript';\n", "var HTML_MIME_TYPE = 'text/html';\n", "var EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\n", "var CLASS_NAME = 'output';\n", "\n", "/**\n", " * Render data to the DOM node\n", " */\n", "function render(props, node) {\n", " var div = document.createElement(\"div\");\n", " var script = document.createElement(\"script\");\n", " node.appendChild(div);\n", " node.appendChild(script);\n", "}\n", "\n", "/**\n", " * Handle when a new output is added\n", " */\n", "function handle_add_output(event, handle) {\n", " var output_area = handle.output_area;\n", " var output = handle.output;\n", " if ((output.data == undefined) || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n", " return\n", " }\n", " var id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n", " var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n", " if (id !== undefined) {\n", " var nchildren = toinsert.length;\n", " var html_node = toinsert[nchildren-1].children[0];\n", " html_node.innerHTML = output.data[HTML_MIME_TYPE];\n", " var scripts = [];\n", " var nodelist = html_node.querySelectorAll(\"script\");\n", " for (var i in nodelist) {\n", " if (nodelist.hasOwnProperty(i)) {\n", " scripts.push(nodelist[i])\n", " }\n", " }\n", "\n", " scripts.forEach( function (oldScript) {\n", " var newScript = document.createElement(\"script\");\n", " var attrs = [];\n", " var nodemap = oldScript.attributes;\n", " for (var j in nodemap) {\n", " if (nodemap.hasOwnProperty(j)) {\n", " attrs.push(nodemap[j])\n", " }\n", " }\n", " attrs.forEach(function(attr) { newScript.setAttribute(attr.name, attr.value) });\n", " newScript.appendChild(document.createTextNode(oldScript.innerHTML));\n", " oldScript.parentNode.replaceChild(newScript, oldScript);\n", " });\n", " if (JS_MIME_TYPE in output.data) {\n", " toinsert[nchildren-1].children[1].textContent = output.data[JS_MIME_TYPE];\n", " }\n", " output_area._hv_plot_id = id;\n", " if ((window.Bokeh !== undefined) && (id in Bokeh.index)) {\n", " window.PyViz.plot_index[id] = Bokeh.index[id];\n", " } else {\n", " window.PyViz.plot_index[id] = null;\n", " }\n", " } else if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n", " var bk_div = document.createElement(\"div\");\n", " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", " var script_attrs = bk_div.children[0].attributes;\n", " for (var i = 0; i < script_attrs.length; i++) {\n", " toinsert[toinsert.length - 1].childNodes[1].setAttribute(script_attrs[i].name, script_attrs[i].value);\n", " }\n", " // store reference to server id on output_area\n", " output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n", " }\n", "}\n", "\n", "/**\n", " * Handle when an output is cleared or removed\n", " */\n", "function handle_clear_output(event, handle) {\n", " var id = handle.cell.output_area._hv_plot_id;\n", " var server_id = handle.cell.output_area._bokeh_server_id;\n", " if (((id === undefined) || !(id in PyViz.plot_index)) && (server_id !== undefined)) { return; }\n", " var comm = window.PyViz.comm_manager.get_client_comm(\"hv-extension-comm\", \"hv-extension-comm\", function () {});\n", " if (server_id !== null) {\n", " comm.send({event_type: 'server_delete', 'id': server_id});\n", " return;\n", " } else if (comm !== null) {\n", " comm.send({event_type: 'delete', 'id': id});\n", " }\n", " delete PyViz.plot_index[id];\n", " if ((window.Bokeh !== undefined) & (id in window.Bokeh.index)) {\n", " var doc = window.Bokeh.index[id].model.document\n", " doc.clear();\n", " const i = window.Bokeh.documents.indexOf(doc);\n", " if (i > -1) {\n", " window.Bokeh.documents.splice(i, 1);\n", " }\n", " }\n", "}\n", "\n", "/**\n", " * Handle kernel restart event\n", " */\n", "function handle_kernel_cleanup(event, handle) {\n", " delete PyViz.comms[\"hv-extension-comm\"];\n", " window.PyViz.plot_index = {}\n", "}\n", "\n", "/**\n", " * Handle update_display_data messages\n", " */\n", "function handle_update_output(event, handle) {\n", " handle_clear_output(event, {cell: {output_area: handle.output_area}})\n", " handle_add_output(event, handle)\n", "}\n", "\n", "function register_renderer(events, OutputArea) {\n", " function append_mime(data, metadata, element) {\n", " // create a DOM node to render to\n", " var toinsert = this.create_output_subarea(\n", " metadata,\n", " CLASS_NAME,\n", " EXEC_MIME_TYPE\n", " );\n", " this.keyboard_manager.register_events(toinsert);\n", " // Render to node\n", " var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n", " render(props, toinsert[0]);\n", " element.append(toinsert);\n", " return toinsert\n", " }\n", "\n", " events.on('output_added.OutputArea', handle_add_output);\n", " events.on('output_updated.OutputArea', handle_update_output);\n", " events.on('clear_output.CodeCell', handle_clear_output);\n", " events.on('delete.Cell', handle_clear_output);\n", " events.on('kernel_ready.Kernel', handle_kernel_cleanup);\n", "\n", " OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n", " safe: true,\n", " index: 0\n", " });\n", "}\n", "\n", "if (window.Jupyter !== undefined) {\n", " try {\n", " var events = require('base/js/events');\n", " var OutputArea = require('notebook/js/outputarea').OutputArea;\n", " if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n", " register_renderer(events, OutputArea);\n", " }\n", " } catch(err) {\n", " }\n", "}\n" ], "application/vnd.holoviews_load.v0+json": "\nif ((window.PyViz === undefined) || (window.PyViz instanceof HTMLElement)) {\n window.PyViz = {comms: {}, comm_status:{}, kernels:{}, receivers: {}, plot_index: []}\n}\n\n\n function JupyterCommManager() {\n }\n\n JupyterCommManager.prototype.register_target = function(plot_id, comm_id, msg_handler) {\n if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n comm_manager.register_target(comm_id, function(comm) {\n comm.on_msg(msg_handler);\n });\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n window.PyViz.kernels[plot_id].registerCommTarget(comm_id, function(comm) {\n comm.onMsg = msg_handler;\n });\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n google.colab.kernel.comms.registerTarget(comm_id, (comm) => {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n console.log(message)\n var content = {data: message.data, comm_id};\n var buffers = []\n for (var buffer of message.buffers || []) {\n buffers.push(new DataView(buffer))\n }\n var metadata = message.metadata || {};\n var msg = {content, buffers, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n })\n }\n }\n\n JupyterCommManager.prototype.get_client_comm = function(plot_id, comm_id, msg_handler) {\n if (comm_id in window.PyViz.comms) {\n return window.PyViz.comms[comm_id];\n } else if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n var comm = comm_manager.new_comm(comm_id, {}, {}, {}, comm_id);\n if (msg_handler) {\n comm.on_msg(msg_handler);\n }\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n var comm = window.PyViz.kernels[plot_id].connectToComm(comm_id);\n comm.open();\n if (msg_handler) {\n comm.onMsg = msg_handler;\n }\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n var comm_promise = google.colab.kernel.comms.open(comm_id)\n comm_promise.then((comm) => {\n window.PyViz.comms[comm_id] = comm;\n if (msg_handler) {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n var content = {data: message.data};\n var metadata = message.metadata || {comm_id};\n var msg = {content, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n }\n }) \n var sendClosure = (data, metadata, buffers, disposeOnDone) => {\n return comm_promise.then((comm) => {\n comm.send(data, metadata, buffers, disposeOnDone);\n });\n };\n var comm = {\n send: sendClosure\n };\n }\n window.PyViz.comms[comm_id] = comm;\n return comm;\n }\n window.PyViz.comm_manager = new JupyterCommManager();\n \n\n\nvar JS_MIME_TYPE = 'application/javascript';\nvar HTML_MIME_TYPE = 'text/html';\nvar EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\nvar CLASS_NAME = 'output';\n\n/**\n * Render data to the DOM node\n */\nfunction render(props, node) {\n var div = document.createElement(\"div\");\n var script = document.createElement(\"script\");\n node.appendChild(div);\n node.appendChild(script);\n}\n\n/**\n * Handle when a new output is added\n */\nfunction handle_add_output(event, handle) {\n var output_area = handle.output_area;\n var output = handle.output;\n if ((output.data == undefined) || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n return\n }\n var id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n if (id !== undefined) {\n var nchildren = toinsert.length;\n var html_node = toinsert[nchildren-1].children[0];\n html_node.innerHTML = output.data[HTML_MIME_TYPE];\n var scripts = [];\n var nodelist = html_node.querySelectorAll(\"script\");\n for (var i in nodelist) {\n if (nodelist.hasOwnProperty(i)) {\n scripts.push(nodelist[i])\n }\n }\n\n scripts.forEach( function (oldScript) {\n var newScript = document.createElement(\"script\");\n var attrs = [];\n var nodemap = oldScript.attributes;\n for (var j in nodemap) {\n if (nodemap.hasOwnProperty(j)) {\n attrs.push(nodemap[j])\n }\n }\n attrs.forEach(function(attr) { newScript.setAttribute(attr.name, attr.value) });\n newScript.appendChild(document.createTextNode(oldScript.innerHTML));\n oldScript.parentNode.replaceChild(newScript, oldScript);\n });\n if (JS_MIME_TYPE in output.data) {\n toinsert[nchildren-1].children[1].textContent = output.data[JS_MIME_TYPE];\n }\n output_area._hv_plot_id = id;\n if ((window.Bokeh !== undefined) && (id in Bokeh.index)) {\n window.PyViz.plot_index[id] = Bokeh.index[id];\n } else {\n window.PyViz.plot_index[id] = null;\n }\n } else if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n var bk_div = document.createElement(\"div\");\n bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n var script_attrs = bk_div.children[0].attributes;\n for (var i = 0; i < script_attrs.length; i++) {\n toinsert[toinsert.length - 1].childNodes[1].setAttribute(script_attrs[i].name, script_attrs[i].value);\n }\n // store reference to server id on output_area\n output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n }\n}\n\n/**\n * Handle when an output is cleared or removed\n */\nfunction handle_clear_output(event, handle) {\n var id = handle.cell.output_area._hv_plot_id;\n var server_id = handle.cell.output_area._bokeh_server_id;\n if (((id === undefined) || !(id in PyViz.plot_index)) && (server_id !== undefined)) { return; }\n var comm = window.PyViz.comm_manager.get_client_comm(\"hv-extension-comm\", \"hv-extension-comm\", function () {});\n if (server_id !== null) {\n comm.send({event_type: 'server_delete', 'id': server_id});\n return;\n } else if (comm !== null) {\n comm.send({event_type: 'delete', 'id': id});\n }\n delete PyViz.plot_index[id];\n if ((window.Bokeh !== undefined) & (id in window.Bokeh.index)) {\n var doc = window.Bokeh.index[id].model.document\n doc.clear();\n const i = window.Bokeh.documents.indexOf(doc);\n if (i > -1) {\n window.Bokeh.documents.splice(i, 1);\n }\n }\n}\n\n/**\n * Handle kernel restart event\n */\nfunction handle_kernel_cleanup(event, handle) {\n delete PyViz.comms[\"hv-extension-comm\"];\n window.PyViz.plot_index = {}\n}\n\n/**\n * Handle update_display_data messages\n */\nfunction handle_update_output(event, handle) {\n handle_clear_output(event, {cell: {output_area: handle.output_area}})\n handle_add_output(event, handle)\n}\n\nfunction register_renderer(events, OutputArea) {\n function append_mime(data, metadata, element) {\n // create a DOM node to render to\n var toinsert = this.create_output_subarea(\n metadata,\n CLASS_NAME,\n EXEC_MIME_TYPE\n );\n this.keyboard_manager.register_events(toinsert);\n // Render to node\n var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n render(props, toinsert[0]);\n element.append(toinsert);\n return toinsert\n }\n\n events.on('output_added.OutputArea', handle_add_output);\n events.on('output_updated.OutputArea', handle_update_output);\n events.on('clear_output.CodeCell', handle_clear_output);\n events.on('delete.Cell', handle_clear_output);\n events.on('kernel_ready.Kernel', handle_kernel_cleanup);\n\n OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n safe: true,\n index: 0\n });\n}\n\nif (window.Jupyter !== undefined) {\n try {\n var events = require('base/js/events');\n var OutputArea = require('notebook/js/outputarea').OutputArea;\n if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n register_renderer(events, OutputArea);\n }\n } catch(err) {\n }\n}\n" }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.holoviews_exec.v0+json": "", "text/html": [ "
\n", "
\n", "
\n", "" ] }, "metadata": { "application/vnd.holoviews_exec.v0+json": { "id": "p1002" } }, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": {}, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.holoviews_exec.v0+json": "", "text/html": [ "
\n", "
\n", "
\n", "" ], "text/plain": [ ":NdOverlay [Variable]\n", " :Curve [Local Timestamp] (value)" ] }, "execution_count": 10, "metadata": { "application/vnd.holoviews_exec.v0+json": { "id": "p1004" } }, "output_type": "execute_result" } ], "source": [ "import holoviews as hv\n", "\n", "hv.extension('bokeh')\n", "\n", "df.plot(x='Local Timestamp')" ] }, { "cell_type": "markdown", "id": "d5a9d4a3", "metadata": {}, "source": [ "## Display last trades between the step" ] }, { "cell_type": "code", "execution_count": 11, "id": "71dd4f1a", "metadata": {}, "outputs": [], "source": [ "from hftbacktest import BUY_EVENT\n", "\n", "@njit\n", "def print_trades(hbt):\n", " while hbt.elapse(60 * 1e9) == 0:\n", " print('-------------------------------------------------------------------------------')\n", " print('current_timestamp:', hbt.current_timestamp)\n", "\n", " # Gets the last trades occurring in the market, not the trades of our orders.\n", " last_trades = hbt.last_trades(0)\n", " \n", " num = 0\n", " for last_trade in last_trades:\n", " if num > 10:\n", " print('...')\n", " break\n", " print(\n", " 'exch_timestamp:',\n", " last_trade.exch_ts,\n", " 'buy' if (last_trade.ev & BUY_EVENT) == BUY_EVENT else 'sell',\n", " last_trade.qty,\n", " '@',\n", " last_trade.px\n", " )\n", " num += 1\n", "\n", " # To prevent accumulating all last trades, which may cause a slowdown,\n", " # clear_last_trades needs to be called.\n", " # After this, accessing `last_trades` will cause a crash.\n", " hbt.clear_last_trades(0)\n", " return True" ] }, { "cell_type": "code", "execution_count": 12, "id": "0d37656a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-------------------------------------------------------------------------------\n", "current_timestamp: 1723161661500000000\n", "exch_timestamp: 1723161602372000000 buy 0.489 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.198 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.006 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.002 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.003 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.011 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.238 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.007 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.005 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.003 @ 61659.8\n", "exch_timestamp: 1723161602372000000 buy 0.002 @ 61659.8\n", "...\n", "-------------------------------------------------------------------------------\n", "current_timestamp: 1723161721500000000\n", "exch_timestamp: 1723161661697000000 sell 0.002 @ 61594.1\n", "exch_timestamp: 1723161661724000000 sell 0.002 @ 61594.1\n", "exch_timestamp: 1723161661751000000 buy 0.135 @ 61594.2\n", "exch_timestamp: 1723161661806000000 sell 1.328 @ 61594.1\n", "exch_timestamp: 1723161661806000000 sell 0.002 @ 61594.1\n", "exch_timestamp: 1723161661806000000 sell 0.002 @ 61594.1\n", "exch_timestamp: 1723161661806000000 sell 0.002 @ 61594.1\n", "exch_timestamp: 1723161661806000000 sell 0.006 @ 61594.1\n", "exch_timestamp: 1723161661806000000 sell 0.32 @ 61594.1\n", "exch_timestamp: 1723161661806000000 sell 0.032 @ 61594.1\n", "exch_timestamp: 1723161661806000000 sell 1.208 @ 61594.1\n", "...\n", "-------------------------------------------------------------------------------\n", "current_timestamp: 1723161781500000000\n", "exch_timestamp: 1723161721541000000 sell 0.002 @ 61576.5\n", "exch_timestamp: 1723161721574000000 buy 0.012 @ 61576.6\n", "exch_timestamp: 1723161721578000000 sell 0.003 @ 61576.5\n", "exch_timestamp: 1723161721583000000 buy 0.275 @ 61576.6\n", "exch_timestamp: 1723161721583000000 buy 0.469 @ 61576.6\n", "exch_timestamp: 1723161721585000000 buy 0.095 @ 61576.6\n", "exch_timestamp: 1723161721585000000 buy 0.102 @ 61576.6\n", "exch_timestamp: 1723161721585000000 buy 0.197 @ 61576.6\n", "exch_timestamp: 1723161721586000000 buy 0.13 @ 61576.6\n", "exch_timestamp: 1723161721587000000 buy 0.425 @ 61576.6\n", "exch_timestamp: 1723161721587000000 buy 0.324 @ 61576.6\n", "...\n", "-------------------------------------------------------------------------------\n", "current_timestamp: 1723161841500000000\n", "exch_timestamp: 1723161781628000000 sell 0.026 @ 61629.6\n", "exch_timestamp: 1723161781727000000 buy 0.011 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.05 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.006 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.002 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.007 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.002 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.075 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.065 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.247 @ 61629.7\n", "exch_timestamp: 1723161781727000000 buy 0.002 @ 61629.7\n", "...\n", "-------------------------------------------------------------------------------\n", "current_timestamp: 1723161901500000000\n", "exch_timestamp: 1723161841561000000 buy 0.01 @ 61621.6\n", "exch_timestamp: 1723161841561000000 buy 0.006 @ 61621.6\n", "exch_timestamp: 1723161841561000000 buy 0.002 @ 61621.6\n", "exch_timestamp: 1723161841561000000 buy 0.022 @ 61621.6\n", "exch_timestamp: 1723161841561000000 buy 0.097 @ 61621.6\n", "exch_timestamp: 1723161841561000000 buy 0.024 @ 61621.6\n", "exch_timestamp: 1723161841564000000 buy 0.024 @ 61621.6\n", "exch_timestamp: 1723161841564000000 buy 0.014 @ 61621.6\n", "exch_timestamp: 1723161841565000000 buy 0.003 @ 61621.6\n", "exch_timestamp: 1723161841613000000 buy 0.002 @ 61622.5\n", "exch_timestamp: 1723161841613000000 buy 0.003 @ 61622.6\n", "...\n" ] } ], "source": [ "asset = (\n", " BacktestAsset()\n", " .data(btcusdt_20240809)\n", " .initial_snapshot(btcusdt_20240808_eod)\n", " .linear_asset(1.0) \n", " .constant_latency(10_000_000, 10_000_000)\n", " .risk_adverse_queue_model() \n", " .no_partial_fill_exchange()\n", " .trading_value_fee_model(0.0002, 0.0007)\n", " .tick_size(0.1)\n", " .lot_size(0.001)\n", " # To retrieve the last trades, `last_trades_capacity` should be set.\n", " .last_trades_capacity(1000)\n", " .roi_lb(30000)\n", " .roi_ub(90000)\n", ")\n", "\n", "hbt = ROIVectorMarketDepthBacktest([asset])\n", "\n", "print_trades(hbt)\n", "\n", "_ = hbt.close()" ] }, { "cell_type": "markdown", "id": "a37694e1", "metadata": {}, "source": [ "## Rolling Volume-Weighted Average Price" ] }, { "cell_type": "code", "execution_count": 13, "id": "002fa805", "metadata": {}, "outputs": [], "source": [ "@njit\n", "def rolling_vwap(hbt, out):\n", " buy_amount_bin = np.zeros(100_000, np.float64)\n", " buy_qty_bin = np.zeros(100_000, np.float64)\n", " sell_amount_bin = np.zeros(100_000, np.float64)\n", " sell_qty_bin = np.zeros(100_000, np.float64)\n", " \n", " idx = 0\n", " last_trade_price = np.nan\n", " \n", " while hbt.elapse(10 * 1e9) == 0:\n", " last_trades = hbt.last_trades(0)\n", " \n", " for last_trade in last_trades:\n", " if (last_trade.ev & BUY_EVENT) == BUY_EVENT:\n", " buy_amount_bin[idx] += last_trade.px * last_trade.qty\n", " buy_qty_bin[idx] += last_trade.qty\n", " else:\n", " sell_amount_bin[idx] += last_trade.px * last_trade.qty\n", " sell_qty_bin[idx] += last_trade.qty\n", " \n", " hbt.clear_last_trades(0)\n", " idx += 1\n", "\n", " if idx >= 1:\n", " vwap10sec = np.divide(\n", " buy_amount_bin[idx - 1] + sell_amount_bin[idx - 1], \n", " buy_qty_bin[idx - 1] + sell_qty_bin[idx - 1]\n", " )\n", " else:\n", " vwap10sec = np.nan\n", " \n", " if idx >= 6:\n", " vwap1m = np.divide(\n", " np.sum(buy_amount_bin[idx - 6:idx]) + np.sum(sell_amount_bin[idx - 6:idx]), \n", " np.sum(buy_qty_bin[idx - 6:idx]) + np.sum(sell_qty_bin[idx - 6:idx])\n", " )\n", " buy_vwap1m = np.divide(np.sum(buy_amount_bin[idx - 6:idx]), np.sum(buy_qty_bin[idx - 6:idx]))\n", " sell_vwap1m = np.divide(np.sum(sell_amount_bin[idx - 6:idx]), np.sum(sell_qty_bin[idx - 6:idx]))\n", " else:\n", " vwap1m = np.nan\n", " buy_vwap1m = np.nan\n", " sell_vwap1m = np.nan\n", " \n", " out.append((hbt.current_timestamp, vwap10sec, vwap1m, buy_vwap1m, sell_vwap1m))\n", " return True" ] }, { "cell_type": "code", "execution_count": 14, "id": "f90739a8", "metadata": {}, "outputs": [], "source": [ "hbt = ROIVectorMarketDepthBacktest([asset])\n", "\n", "tup_ty = Tuple((float64, float64, float64, float64, float64))\n", "out = List.empty_list(tup_ty, allocated=100_000)\n", "\n", "rolling_vwap(hbt, out)\n", "\n", "_ = hbt.close()" ] }, { "cell_type": "code", "execution_count": 15, "id": "f2ab6bd3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "shape: (30, 5)
Local Timestamp10-sec VWAP1-min VWAP1-min Buy VWAP1-min Sell VWAP
datetime[ns]f64f64f64f64
2024-08-09 00:00:11.50061687.182976NaNNaNNaN
2024-08-09 00:00:21.50061709.337576NaNNaNNaN
2024-08-09 00:00:31.50061697.538054NaNNaNNaN
2024-08-09 00:00:41.50061663.958879NaNNaNNaN
2024-08-09 00:00:51.50061637.340621NaNNaNNaN
2024-08-09 00:04:21.50061643.00984761624.45901161626.49554261622.549429
2024-08-09 00:04:31.50061670.79568561635.87725161638.36231461632.48854
2024-08-09 00:04:41.50061643.10858261641.84648961648.67233761636.032054
2024-08-09 00:04:51.50061614.72356961640.49084161647.76984461634.372128
2024-08-09 00:05:01.50061584.69746761637.33410261642.20955161632.12064
" ], "text/plain": [ "shape: (30, 5)\n", "┌─────────────────────────┬──────────────┬──────────────┬────────────────┬─────────────────┐\n", "│ Local Timestamp ┆ 10-sec VWAP ┆ 1-min VWAP ┆ 1-min Buy VWAP ┆ 1-min Sell VWAP │\n", "│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", "│ datetime[ns] ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n", "╞═════════════════════════╪══════════════╪══════════════╪════════════════╪═════════════════╡\n", "│ 2024-08-09 00:00:11.500 ┆ 61687.182976 ┆ NaN ┆ NaN ┆ NaN │\n", "│ 2024-08-09 00:00:21.500 ┆ 61709.337576 ┆ NaN ┆ NaN ┆ NaN │\n", "│ 2024-08-09 00:00:31.500 ┆ 61697.538054 ┆ NaN ┆ NaN ┆ NaN │\n", "│ 2024-08-09 00:00:41.500 ┆ 61663.958879 ┆ NaN ┆ NaN ┆ NaN │\n", "│ 2024-08-09 00:00:51.500 ┆ 61637.340621 ┆ NaN ┆ NaN ┆ NaN │\n", "│ … ┆ … ┆ … ┆ … ┆ … │\n", "│ 2024-08-09 00:04:21.500 ┆ 61643.009847 ┆ 61624.459011 ┆ 61626.495542 ┆ 61622.549429 │\n", "│ 2024-08-09 00:04:31.500 ┆ 61670.795685 ┆ 61635.877251 ┆ 61638.362314 ┆ 61632.48854 │\n", "│ 2024-08-09 00:04:41.500 ┆ 61643.108582 ┆ 61641.846489 ┆ 61648.672337 ┆ 61636.032054 │\n", "│ 2024-08-09 00:04:51.500 ┆ 61614.723569 ┆ 61640.490841 ┆ 61647.769844 ┆ 61634.372128 │\n", "│ 2024-08-09 00:05:01.500 ┆ 61584.697467 ┆ 61637.334102 ┆ 61642.209551 ┆ 61632.12064 │\n", "└─────────────────────────┴──────────────┴──────────────┴────────────────┴─────────────────┘" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pl.DataFrame(out).transpose()\n", "df.columns = ['Local Timestamp', '10-sec VWAP', '1-min VWAP', '1-min Buy VWAP', '1-min Sell VWAP']\n", "df = df.with_columns(\n", " pl.from_epoch('Local Timestamp', time_unit='ns')\n", ")\n", "\n", "df" ] }, { "cell_type": "code", "execution_count": 16, "id": "f23d4a4e", "metadata": {}, "outputs": [ { "data": {}, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.holoviews_exec.v0+json": "", "text/html": [ "
\n", "
\n", "
\n", "" ], "text/plain": [ ":NdOverlay [Variable]\n", " :Curve [Local Timestamp] (value)" ] }, "execution_count": 16, "metadata": { "application/vnd.holoviews_exec.v0+json": { "id": "p1122" } }, "output_type": "execute_result" } ], "source": [ "df.plot(x='Local Timestamp')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }